/**
 * 禁止输入空格
 */
function preventSpace() {
	let event = window.event;
	
	if(event.keyCode === 32) {
		event.returnValue = false;
	}
}

/**
 * 生成随机字符串
 * @param length 字符串的长度，默认11
 * @returns {string}
 */
function generateRandomString(length = 11) {
	let charset = "abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	let values = new Uint32Array(length);

	window.crypto.getRandomValues(values);

	let str = "";

	for (let i = 0; i < length; i++) {
		str += charset[values[i] % charset.length];
	}

	return str;
}

/**
 * 登录
 */
function login() {
	let username = $("#username").val();
    let password = $("#password").val();
    let code = $("#code").val();

	if (!username) {
		alert("请输入用户名！");
		
		$("#username").focus();
	} else if (!password) {
		alert("请输入密码！");
		
		$("#password").focus();
	} else if (!code) {
		alert("请输入验证码！");
	} else {
		ajaxPost("/user/login", {
			username: username,
			password: password,
			code: code,
			uuid: getStorage("uuid")
		}, function() {
			location.href = "/index.html";
		}, error);
	}
}

$(function() {
	$("#username").keydown(function() {
		preventSpace();
	}).attr("placeholder", "请输入用户名");

	$("#password").keydown(function() {
		preventSpace();
	}).attr("placeholder", "请输入密码");

	/**
	 * 给验证码输入框绑定回车登录事件
	 */
	$("#code").keydown(function() {
		let event = window.event;

		if(event.keyCode === 32) {
			event.returnValue = false;
		} else if(event.keyCode === 13) {
			login();
		}
	}).attr("placeholder", "验证码");

	/******************************************************************************************************/

	/*
	 * 验证码初始化
	 */
	// 从localStorage中获取uuid
	let uuid = getStorage("uuid");

	// uuid为空，则生成后保存到localStorage中
	if (!uuid) {
		// 生成uuid
		uuid = generateRandomString();

		// 保存uuid到localStorage
		storage("uuid", uuid);
	}

	let captcha_ = $("#captcha");

	// 设置验证码的图片路径
	captcha_.attr("src", "/captcha/generate?type=png&uuid=" + uuid);
	// 设置验证码的title
	captcha_.attr("title", "看不清？换一张");

	// 点击验证码刷新
	captcha_.click(function () {
		// 生成uuid
		uuid = generateRandomString();

		// 保存uuid到localStorage
		storage("uuid", uuid);

		// 重新设置验证码图片的路径
		$("#captcha").attr("src", "/captcha/generate?v=" + new Date().getTime() + "&type=png&uuid=" + uuid);
	});

	/******************************************************************************************************/

	// 点击登录按钮
	$("#btn_Login").click(function () {
		login();
	});

	$(".content .con_right .left").on("click", function () {
		$(this).css({
			"color": "#333333",
			"border-bottom": "2px solid #2e558e"
		});
		$(".content .con_right .right").css({
			"color": "#999999",
			"border-bottom": "2px solid #dedede"
		});
		$(".content .con_right ul .con_r_left").css("display", "block");
		$(".content .con_right ul .con_r_right").css("display", "none");
	});

	$(".content .con_right .right").on("click", function () {
		$(this).css({
			"color": "#333333",
			"border-bottom": "2px solid #2e558e"
		});
		$(".content .con_right .left").css({
			"color": "#999999",
			"border-bottom": "2px solid #dedede"
		});
		$(".content .con_right ul .con_r_right").css("display", "block");
		$(".content .con_right ul .con_r_left").css("display", "none");
	});

});